//============================================================================
// Name        : main.cpp
// Author      : Daniel Toepel
// Version     : 1.0
// Copyright   : D.Toepel 2009 GPLv3 (General Public License Version 3.0)
// Description : start-application for kybernetes
//============================================================================
#include <iostream>
#include <cstdlib>
#include <dirent.h>
#include <string.h>

using namespace std;

int main(void)
{
    /**
    * Switch OperatingSystem
    * Linux = 0
    * Windows = 1
    * Mac OS = 2
    */
    int os = 0;
    string cmd;
    string classpath = "./app/";

    DIR           *d;
    struct dirent *dir;

    d = opendir("app");

    if (d)
    {
        while ((dir = readdir(d)) != NULL)
        {
            if( strcmp( dir->d_name, "." ) == 0 || strcmp( dir->d_name, ".." ) == 0 ) {
                continue;
            }

            string filename = dir->d_name;
            if(filename.substr(filename.find_last_of(".") + 1) == "jar") {
            classpath.append(";./app/" + filename);
            }
        }

        closedir(d);
    }

    switch (os){
        // Windows
        // -Djava.rmi.server.codebase=\"file:/app/kcRMI.jar\"
        case 1 : cmd = "start \"java -jar\" \"app\\kybernetes.jar\" \"-cp\" \"" + classpath  + "\"";
        break;

        // Linux
        // -Djava.rmi.server.codebase=\"file:/app/kcRMI.jar\"
        default: cmd = "java -jar app/kybernetes.jar \"-cp\" \"" + classpath  + "\"";
        break;
    }

    char * cmdBuffer = new char[cmd.length()+1];
	strcpy(cmdBuffer,cmd.c_str());

    int i = system(cmdBuffer);

    return i;
}
